package cz.muni.fi.pa165.travelagency.web.frontend;

import com.samaxes.stripes.ejb3.EJBBean;
import cz.muni.fi.pa165.travelagency.api.entities.ITour;
import cz.muni.fi.pa165.travelagency.api.services.ITourService;
import cz.muni.fi.pa165.travelagency.dto.TourDTO;
import javax.ejb.EJB;
import net.sourceforge.stripes.action.ActionBean;
import net.sourceforge.stripes.action.ActionBeanContext;
import net.sourceforge.stripes.action.Before;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;
import net.sourceforge.stripes.controller.LifecycleStage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


@UrlBinding("/tour-view/{$event}/{tour.id}")
@EJB(name = "tourService", beanInterface = ITourService.class)
public class TourActionBean implements ActionBean {
    
    final static Logger log = LoggerFactory.getLogger(PackageTourActionBean.class);
    private ActionBeanContext context;
    private TourDTO tour;
    
    @EJBBean("java:comp/env/tourService")
    private ITourService service;

    @Override
    public void setContext(ActionBeanContext context) {
        this.context = context;
    }

    @Override
    public ActionBeanContext getContext() {
        return context;
    }

    public TourDTO getTour() {
        return tour;
    }

    @Before(stages = LifecycleStage.BindingAndValidation, on = {"detail"})
    public void loadTourFromDatabase() {
        String id = context.getRequest().getParameter("tour.id");
        if (id == null) {
            return;
        }
        tour = (TourDTO) service.getTour(Long.parseLong(id));
    }

    @DefaultHandler
    public Resolution detail() {
        return new ForwardResolution("/frontend/tour_detail.jsp");
    }
 }
